<?php
/***************************************************************************
 *                           usercp_viewprofile.php
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id: usercp_viewprofile.php,v 1.5.2.6 2005/09/14 18:14:30 acydburn Exp $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *
 ***************************************************************************/

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
	exit;
}

include('includes/bbcode.'.$phpEx);
include('includes/functions_comments.'.$phpEx);

if ( empty($HTTP_GET_VARS[POST_USERS_URL]) || $HTTP_GET_VARS[POST_USERS_URL] == ANONYMOUS )
{
	message_die(GENERAL_MESSAGE, $lang['No_user_id_specified']);
}
$profiledata = get_userdata($HTTP_GET_VARS[POST_USERS_URL]);
if (!$profiledata)
{
	message_die(GENERAL_MESSAGE, $lang['No_user_id_specified']);
}

$sql = "SELECT *
	FROM " . RANKS_TABLE . "
	ORDER BY rank_special, rank_min";
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, 'Could not obtain ranks information', '', __LINE__, __FILE__, $sql);
}

$ranksrow = array();
while ( $row = $db->sql_fetchrow($result) )
{
	$ranksrow[] = $row;
}
$db->sql_freeresult($result);

//
// Output page header and profile_view template
//
$template->set_filenames(array(
	'body' => 'profile_view_body.tpl')
);
if (is_active("Forums")) {
    make_jumpbox('viewforum.'.$phpEx);
}
//
// Calculate the number of days this user has been a member ($memberdays)
// Then calculate their posts per day
//
$regdate = $profiledata['user_regdate'];
$nukedate = strtotime($regdate);
$memberdays = max(1, round( ( time() - $nukedate ) / 86400 ));
$posts_per_day = $profiledata['user_posts'] / $memberdays;

// Get the users percentage of total posts
if ( $profiledata['user_posts'] != 0  )
{
	$total_posts = get_db_stat('postcount');
	$percentage = ( $total_posts ) ? min(100, ($profiledata['user_posts'] / $total_posts) * 100) : 0;
}
else
{
	$percentage = 0;
}

$avatar_img = '';
if ( $profiledata['user_avatar_type'] && $profiledata['user_allowavatar'] )
{
	switch( $profiledata['user_avatar_type'] )
	{
		case USER_AVATAR_UPLOAD:
			$avatar_img = ( $board_config['allow_avatar_upload'] ) ? '<img src="' . $board_config['avatar_path'] . '/' . $profiledata['user_avatar'] . '" alt="" border="0" />' : '';
			break;
		case USER_AVATAR_REMOTE:
			$avatar_img = ( $board_config['allow_avatar_remote'] ) ? '<img src="' . $profiledata['user_avatar'] . '" alt="" border="0" />' : '';
			break;
		case USER_AVATAR_GALLERY:
			$avatar_img = ( $board_config['allow_avatar_local'] ) ? '<img src="' . $board_config['avatar_gallery_path'] . '/' . $profiledata['user_avatar'] . '" alt="" border="0" />' : '';
			break;
	}
}

$poster_rank = '';
$rank_image = '';
if ( $profiledata['user_rank'] )
{
	for($i = 0; $i < count($ranksrow); $i++)
	{
		if ( $profiledata['user_rank'] == $ranksrow[$i]['rank_id'] && $ranksrow[$i]['rank_special'] )
		{
			$poster_rank = $ranksrow[$i]['rank_title'];
			$rank_image = ( $ranksrow[$i]['rank_image'] ) ? '<img src="' . $ranksrow[$i]['rank_image'] . '" alt="' . $poster_rank . '" title="' . $poster_rank . '" border="0" /><br />' : '';
		}
	}
}
else
{
	for($i = 0; $i < count($ranksrow); $i++)
	{
		if ( $profiledata['user_posts'] >= $ranksrow[$i]['rank_min'] && !$ranksrow[$i]['rank_special'] )
		{
			$poster_rank = $ranksrow[$i]['rank_title'];
			$rank_image = ( $ranksrow[$i]['rank_image'] ) ? '<img src="' . $ranksrow[$i]['rank_image'] . '" alt="' . $poster_rank . '" title="' . $poster_rank . '" border="0" /><br />' : '';
		}
	}
}

$temp_url = append_sid("privmsg.$phpEx?mode=post&amp;" . POST_USERS_URL . "=" . $profiledata['user_id']);
if (is_active("Private_Messages")) {
$pm_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_pm'] . '" alt="' . $lang['Send_private_message'] . '" title="' . $lang['Send_private_message'] . '" border="0" /></a>';
// FLAGHACK-start
$location = ( $profiledata['user_from'] ) ? $profiledata['user_from'] : '&nbsp;' ;
$flag = ( !empty($profiledata['user_from_flag']) ) ? "&nbsp;<img src=\"modules/Forums/images/flags/" . $profiledata['user_from_flag'] . "\" alt=\"" . $profiledata['user_from_flag'] . "\">" : "";
$location .= $flag ;
// FLAGHACK-end
$pm = '<a href="' . $temp_url . '">' . $lang['Send_private_message'] . '</a>';
}

if ( !empty($profiledata['user_viewemail']) || $userdata['user_level'] == ADMIN )
{
	$email_uri = ( $board_config['board_email_form'] ) ? append_sid("profile.$phpEx?mode=email&amp;" . POST_USERS_URL .'=' . $profiledata['user_id']) : 'mailto:' . $profiledata['user_email'];

	$email_img = '<a href="' . $email_uri . '"><img src="' . $images['icon_email'] . '" alt="' . $lang['Send_email'] . '" title="' . $lang['Send_email'] . '" border="0" /></a>';
	$email = '<a href="' . $email_uri . '">' . $lang['Send_email'] . '</a>';
}
else
{
	$email_img = '&nbsp;';
	$email = '&nbsp;';
}
if (( $profiledata['user-website'] == "http:///") || ( $profiledata['user_website'] == "http://")){
    $profiledata['user_website'] =  "";
}
if (($profiledata['user_website'] != "" ) && (substr($profiledata['user_website'],0, 7) != "http://")) {
    $profiledata['user_website'] = "http://".$profiledata['user_website'];
}

$www_img = ( $profiledata['user_website'] ) ? '<a href="' . $profiledata['user_website'] . '" target="_userwww"><img src="' . $images['icon_www'] . '" alt="' . $lang['Visit_website'] . '" title="' . $lang['Visit_website'] . '" border="0" /></a>' : '&nbsp;';
$www = ( $profiledata['user_website'] ) ? '<a href="' . $profiledata['user_website'] . '" target="_userwww">' . $profiledata['user_website'] . '</a>' : '&nbsp;';

if ( !empty($profiledata['user_icq']) )
{
	$icq_status_img = '<a href="http://wwp.icq.com/' . $profiledata['user_icq'] . '#pager"><img src="http://web.icq.com/whitepages/online?icq=' . $profiledata['user_icq'] . '&img=5" width="18" height="18" border="0" /></a>';
	$icq_img = '<a href="http://wwp.icq.com/scripts/search.dll?to=' . $profiledata['user_icq'] . '"><img src="' . $images['icon_icq'] . '" alt="' . $lang['ICQ'] . '" title="' . $lang['ICQ'] . '" border="0" /></a>';
	$icq =  '<a href="http://wwp.icq.com/scripts/search.dll?to=' . $profiledata['user_icq'] . '">' . $lang['ICQ'] . '</a>';
}
else
{
	$icq_status_img = '&nbsp;';
	$icq_img = '&nbsp;';
	$icq = '&nbsp;';
}

$aim_img = ( $profiledata['user_aim'] ) ? '<a href="aim:goim?screenname=' . $profiledata['user_aim'] . '&amp;message=Hello+Are+you+there?"><img src="' . $images['icon_aim'] . '" alt="' . $lang['AIM'] . '" title="' . $lang['AIM'] . '" border="0" /></a>' : '&nbsp;';
$aim = ( $profiledata['user_aim'] ) ? '<a href="aim:goim?screenname=' . $profiledata['user_aim'] . '&amp;message=Hello+Are+you+there?">' . $lang['AIM'] . '</a>' : '&nbsp;';

$msn_img = ( $profiledata['user_msnm'] ) ? $profiledata['user_msnm'] : '&nbsp;';
$msn = $msn_img;

$yim_img = ( $profiledata['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $profiledata['user_yim'] . '&amp;.src=pg"><img src="' . $images['icon_yim'] . '" alt="' . $lang['YIM'] . '" title="' . $lang['YIM'] . '" border="0" /></a>' : '';
$yim = ( $profiledata['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $profiledata['user_yim'] . '&amp;.src=pg">' . $lang['YIM'] . '</a>' : '';

$temp_url = append_sid("search.$phpEx?search_author=" . urlencode($profiledata['username']) . "&amp;showresults=posts");
$search_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_search'] . '" alt="' . sprintf($lang['Search_user_posts'], $profiledata['username']) . '" title="' . sprintf($lang['Search_user_posts'], $profiledata['username']) . '" border="0" /></a>';
$search = '<a href="' . $temp_url . '">' . sprintf($lang['Search_user_posts'], $profiledata['username']) . '</a>';

/* Begin HP/MP/EXP Mod
 *
 * Note: all new variables used created in this mod
 * are prefixed with level_ to be sure of not overwriting
 * other variables.
 *
 */

/* Calculate Level
 * A user's level is determined by their total number of posts.
 * A nice mathmatical formula is used to translate their post count
 * into an intager level.
 *
 * Note: A user with zero (0) posts is considered level 0, however
 * making one (1) post, raises them to level 1.
 *
 */

if($profiledata['user_posts'] < 1)
{
	$level_level = 0;
}
else
{
	$level_level = floor( pow( log10( $profiledata['user_posts'] ), 3 ) ) + 1;
}

/* Determine Hit Points (HP)
 *
 * Hp is based on user activity.
 * Max HP is based on the user's level, and will generally
 * be the same for all users of the same level.
 *
 * A user's current HP is based on the user's posts per day.
 * A higher post per day (ppd), the more HP they will have. A
 * user with an average PPD (set below) will have 50% of their
 * max HP. As a user goes over the average PPD, they will have
 * more HP, but the gains will decrease as the user's PPD increases.
 * This makes achieving 100% hp difficult, but not impossible.
 *
 * For users with under the average PPD, they will have HP equal
 * to 1/2 the percentage their ppd is of the average.
 * ie- a user with 2.5 ppd, and an average ppd of 5 will have
 * 25% of their max HP. This is because 2.5 is 50% of 5, and 1/2
 * of that, is 25%.
 *
 * Users who manage to post so far above the average that they have
 * more HP than their max will recieve a bonus to their max HP.
 *
 * Note that users at level 0 will always have 0 / 0 hp.
 *
 */

/*
 * This value determines the 'average' posts per day.
 * The admin may redefine this variable as he wishes.
 * Higher values will generally decrease users' HP, and
 * lower values will generally increase users' HP.
 *
 * Note: Do NOT set this value to zero (0).
 * This -may- be set to a fractional value (eg, 5.1, 3.1415)
 *
 */

$level_avg_ppd = 5;

/*
 * this value sets how hard it is to achieve 100%
 * hp. The higher you set it, the harder it is to
 * get full hp.
 *
 * to judge how high to set it, a user must have
 * posts per day equal to the $level_avg_ppd plus
 * the number set below.
 *
 * This should NOT be zero.
 */

$level_bonus_redux = 5;

if($level_level < 1)
{
	$level_hp = "0 / 0";
	$level_hp_percent = 0;
}
else
{
	$level_max_hp = floor( (pow( $level_level, (1/4) ) ) * (pow( 10, pow( $level_level+2, (1/3) ) ) ) / (1.5) );

	if($posts_per_day >= $level_avg_ppd)
	{
		$level_hp_percent = floor( (.5 + (($posts_per_day - $level_avg_ppd) / ($level_bonus_redux * 2)) ) * 100);
	}
	else
	{
		$level_hp_percent = floor( $posts_per_day / ($level_avg_ppd / 50) );
	}

	if($level_hp_percent > 100)
	{
		//Give the user a bonus to max HP for greater than 100% hp.
		$level_max_hp += floor( ($level_hp_percent - 100) * pi() );
		$level_hp_percent = 100;
	}
	else
	{
		$level_hp_percent = max(0, $level_hp_percent);
	}

	$level_cur_hp = floor($level_max_hp * ($level_hp_percent / 100) );

	//Be sure a user has no more than max, and no less than zero hp.
	$level_cur_hp = max(0, $level_cur_hp);
	$level_cur_hp = min($level_max_hp, $level_cur_hp);

	$level_hp = $level_cur_hp . ' / ' . $level_max_hp;
}

/* Determine MP
 *
 * MP is calculated by how long the user has been around
 * and how often they post.
 *
 * Max MP is based on level, and increases with level
 * Each post a user makes costs them mp,
 * and a user regenerates mp proportional to how
 * many days they have been registered
 *
 */

//Number of days the user has been at the forums.
$level_user_days = max(1, round( ( time() - $profiledata['user_regdate'] ) / 86400 ));

/* The mp cost for one post.
 * Raising this value will generally decrease the current
 * mp for most posters.
 * This may be set to a decimal value (eg, 2, 2.1, 3.141596)
 * This should NOT be set to 0
 */
$level_post_mp_cost = 2.5;

/* This determines how much mp a user regenerates per day
 * Raising this value will generally increase the current
 * mp for most posters.
 * This may be set to a decimal value (eg, 3, 3.5, 2.71828)
 * This should NOT be set to 0
 */
$level_mp_regen_per_day = 4;

if($level_level < 1)
{
	$level_mp = '0 / 0';
	$level_mp_percent = 0;
}
else
{
	$level_max_mp = floor( (pow( $level_level, (1/4) ) ) * (pow( 10, pow( $level_level+2, (1/3) ) ) ) / (pi()) );

	$level_mp_cost = $profiledata['user_posts'] * $level_post_mp_cost;
	$level_mp_regen = max(1, $level_user_days * $level_mp_regen_per_day);

	$level_cur_mp = floor($level_max_mp - $level_mp_cost + $level_mp_regen);
	$level_cur_mp = max(0, $level_cur_mp);
	$level_cur_mp = min($level_max_mp, $level_cur_mp);

	$level_mp = $level_cur_mp . ' / ' . $level_max_mp;
	$level_mp_percent = floor($level_cur_mp / $level_max_mp * 100 );

}

/* Determine EXP percentage
 *
 * Experience is determined by how far the user is away
 * from the next level. This is expressed as a percentage.
 *
 * Note, a user of level 0 has 100% experience. Making one post
 * will put them at level 1. Also, a user that is shown to have 100%
 * experience, will go up a level on their next post.
 *
 */

if($level_level == 0)
{
	$level_exp = "0 / 0";
	$level_exp_percent = 100;
}
else
{
	$level_posts_for_next = floor( pow( 10, pow( $level_level, (1/3) ) ) );


	$level_posts_for_this = max(1, floor( pow( 10, pow( ($level_level - 1), (1/3) ) ) ) );



	$level_exp = ($profiledata['user_posts'] - $level_posts_for_this) . " / " . ($level_posts_for_next - $level_posts_for_this);

	$level_exp_percent = floor( ( ($profiledata['user_posts'] - $level_posts_for_this) / max( 1, ($level_posts_for_next - $level_posts_for_this ) ) ) * 100);
}

/* END HP/MP/EXP MOD */

//
// Generate page
//
$page_title = $lang['Viewing_profile'];
include("includes/page_header.php");
$profiledata['user_from'] = str_replace(".gif", "", $profiledata['user_from']);
if (function_exists('get_html_translation_table'))
{
	$u_search_author = urlencode(strtr($profiledata['username'], array_flip(get_html_translation_table(HTML_ENTITIES))));
}
else
{
	$u_search_author = urlencode(str_replace(array('&amp;', '&#039;', '&quot;', '&lt;', '&gt;'), array('&', "'", '"', '<', '>'), $profiledata['username']));
}

$template->assign_vars(array(

#======================================================================= |
#==== Start: == Advanced Username Color ================================ |
#==== v1.0.5 =========================================================== |
#====
	'USERNAME' => CheckUsernameColor($profiledata['user_color_gc'], $profiledata['username']),
#====
#==== Author: aUsTiN [austin@phpbb-amod.com] [http://phpbb-tweaks.com] = |
#==== End: ==== Advanced Username Color ================================ |	
#======================================================================= |	

	// 'USERNAME' => $profiledata['username'],
        'JOINED' => $profiledata['user_regdate'],
	'POSTER_RANK' => $poster_rank,
	'RANK_IMAGE' => $rank_image,
	'POSTS_PER_DAY' => $posts_per_day,
	'POSTS' => $profiledata['user_posts'],
        'PERCENTAGE' => $percentage . '%',
        'POST_DAY_STATS' => sprintf($lang['User_post_day_stats'], $posts_per_day),
        'POST_PERCENT_STATS' => sprintf($lang['User_post_pct_stats'], $percentage),

	'SEARCH_IMG' => $search_img,
	'SEARCH' => $search,
	'PM_IMG' => $pm_img,
	'PM' => $pm,
	'EMAIL_IMG' => $email_img,
	'EMAIL' => $email,
	'WWW_IMG' => $www_img,
	'WWW' => $www,
	'ICQ_STATUS_IMG' => $icq_status_img,
	'ICQ_IMG' => $icq_img, 
	'ICQ' => $icq, 
	'AIM_IMG' => $aim_img,
	'AIM' => $aim,
	'MSN_IMG' => $msn_img,
	'MSN' => $msn,
	'YIM_IMG' => $yim_img,
	'YIM' => $yim,

// FLAGHACK-start
	'LOCATION' => $location,
// FLAGHACK-end
	//'LOCATION' => ( $profiledata['user_from'] ) ? $profiledata['user_from'] : '&nbsp;',
	'OCCUPATION' => ( $profiledata['user_occ'] ) ? $profiledata['user_occ'] : '&nbsp;',
	'INTERESTS' => ( $profiledata['user_interests'] ) ? $profiledata['user_interests'] : '&nbsp;',
	'AVATAR_IMG' => $avatar_img,

#======================================================================= |
#==== Start: == Advanced Username Color ================================ |
#==== v1.0.5 =========================================================== |
#====
	'L_VIEWING_PROFILE' => sprintf($lang['Viewing_user_profile'], CheckUsernameColor($profiledata['user_color_gc'], $profiledata['username'])), 
#====
#==== Author: aUsTiN [austin@phpbb-amod.com] [http://phpbb-tweaks.com] = |
#==== End: ==== Advanced Username Color ================================ |	
#======================================================================= |

        // 'L_VIEWING_PROFILE' => sprintf($lang['Viewing_user_profile'], $profiledata['username']),
        'L_ABOUT_USER' => sprintf($lang['About_user'], $profiledata['username']),
        'L_AVATAR' => $lang['Avatar'],
        'L_POSTER_RANK' => $lang['Poster_rank'],
        'L_JOINED' => $lang['Joined'],
        'L_TOTAL_POSTS' => $lang['Total_posts'],
        'L_SEARCH_USER_POSTS' => sprintf($lang['Search_user_posts'], $profiledata['username']),
	'L_CONTACT' => $lang['Contact'],
	'L_EMAIL_ADDRESS' => $lang['Email_address'],
	'L_EMAIL' => $lang['Email'],
	'L_PM' => $lang['Private_Message'],
	'L_ICQ_NUMBER' => $lang['ICQ'],
	'L_YAHOO' => $lang['YIM'],
	'L_AIM' => $lang['AIM'],
	'L_MESSENGER' => $lang['MSNM'],
	'L_WEBSITE' => $lang['Website'],
	'L_LOCATION' => $lang['Location'],
	'L_OCCUPATION' => $lang['Occupation'],
	'L_INTERESTS' => $lang['Interests'],

	'U_SEARCH_USER' => append_sid("search.$phpEx?search_author=" . $u_search_author),

	/* BEGIN LEVEL MOD */
	'HP' => $level_hp,
	'HP_WIDTH' => $level_hp_percent,
	'HP_EMPTY' => ( 100 - $level_hp_percent ),
	'MP' => $level_mp,
	'MP_WIDTH' => $level_mp_percent,
	'MP_EMPTY' => ( 100 - $level_mp_percent ),
	'EXP' => $level_exp,
	'EXP_WIDTH' => $level_exp_percent,
	'EXP_EMPTY' => ( 100 - $level_exp_percent ),
	'LEVEL' => $level_level,
	/* END LEVEL MOD */

	'S_PROFILE_ACTION' => append_sid("profile.$phpEx"))
);

get_last_comments($profiledata['user_id']);
get_post_comments($profiledata['user_id']);
get_comments_links($profiledata['user_id']);

$cm_viewprofile->post_vars($template,$profiledata,$userdata);

$template->pparse('body');

include("includes/page_tail.php");

?>